package com.app.newiptv.adaper;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.app.newiptv.R;
import com.app.newiptv.activity.FilmDetailsActivity;
import com.app.newiptv.activity.SitcomActivity;
import com.app.newiptv.base.MyApplication;
import com.app.newiptv.presenter.SearchPresenter;


/**
 * Created by Shinelon on 2017/9/24.
 */

public class SearchAdapter extends RecyclerView.Adapter<SearchAdapter.MainRecycleViewHolder> {


    private SearchPresenter presenter;
    private int count;
    private Context mContext;

    public SearchAdapter(SearchPresenter presenter, Context mContext) {
        this.presenter = presenter;
        this.mContext = mContext;
    }

    public void setCount(int count) {
        this.count = count;
    }

    @Override
    public MainRecycleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_search_view, null, false);
        MainRecycleViewHolder mainRecycleViewHolder = new MainRecycleViewHolder(view);
        return mainRecycleViewHolder;
    }

    @Override
    public void onBindViewHolder(MainRecycleViewHolder holder, int position) {
        presenter.onBindMainChanelView(holder, position);
        holder.setPosition(position);
    }

    @Override
    public int getItemCount() {
        return count;
    }

    public class MainRecycleViewHolder extends RecyclerView.ViewHolder implements SearchPresenter.SearchViewHolder {

        private int position;
        private TextView program_name;
        private RelativeLayout program_view;

        public MainRecycleViewHolder(View itemView) {
            super(itemView);
            program_name = (TextView) itemView.findViewById(R.id.program_name);
            program_view = (RelativeLayout) itemView.findViewById(R.id.program_view);
            program_name.setTypeface(MyApplication.face);
            itemView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View view, boolean b) {
                    if (b) {
                        program_view.setBackgroundResource(R.mipmap.dise_middle02);
//                        boolean scroll = FocusedViewUtils.arrowScroll(view);
//                        if(!scroll) {
//                            FocusedViewUtils.setFocusedView(focusview, key_btn_view, 10, 10, 10, 10);
//                        }
                    } else {
                        program_view.setBackgroundResource(0);
//                        FocusedViewUtils.setBlurView(key_btn_view);
//                        focusview.setVisibility(View.INVISIBLE);
                    }
                }
            });
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Bundle bundle = presenter.getParams(position);
                    Intent intent = new Intent();
                    intent.putExtra("bundle", bundle);
                    if (presenter.getVodType(position)) {
                        intent.setClass(mContext, FilmDetailsActivity.class);
                        mContext.startActivity(intent);
                    } else if (presenter.getTelType(position)) {
                        intent.setClass(mContext, SitcomActivity.class);
                        mContext.startActivity(intent);
                    }
                    presenter.searchPosition(position);
                }
            });
        }

        public void setPosition(int position) {
            this.position = position;
        }

        @Override
        public void setprogramName(String programName) {
            program_name.setText(programName);
        }
    }
}
